package com.example.superoa.model.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.dao.KqBusinessDao;
import com.example.superoa.model.dao.StaffDao;
import com.example.superoa.model.mappers.BusinessMapper;
import com.example.superoa.model.pojos.KqBusiness;

@Service
@Transactional(rollbackFor=Exception.class)
public class BusinessService {
	@Autowired
	private BusinessMapper map;
	
	@Autowired
	private KqBusinessDao b_dao;
	
	@Autowired
	private AttendService a_ser;
	
	@Autowired 
	private StaffDao s_dao;
	
	Shijian sj=new Shijian();
	
	public JSONArray selectAll() {
		return map.businessSelectAll();
	}
	
	public JSONObject selectId(int id) {
		return map.selectBusinessId(id);
	}
	
	public Integer selectCount(int id) {
		return map.selectBusinessCount(id,0,0);
	}
	//新增出差申请，判断是否有其他申请
	public Integer selectPan(Date kai,Date jie,Integer staff_id) {
		JSONArray chucha=map.selectMonthAll(staff_id);
		for (Date day : sj.getDays(1, kai, jie)) {
			for (Object object : chucha) {
				JSONObject obj=(JSONObject)object;
				if(sj.belongCalendar(day, obj.getDate("bus_kai"), obj.getDate("bus_jie"), 2)) {
					return 1;
				}
			}
		}
		return 0;
				
	}
	//新增出差记录,判断是否有未申请原因来决定是否为补录
	public void addChu(KqBusiness business,int staff_id) {
		if(business.getBusNowhy()==null||business.getBusNowhy().equals("")) {
			business.setBusChu(1);
		}else {
			business.setBusChu(2);
		}
		business.setStaff(s_dao.findById(staff_id).get());
		business.setBusShen(0);
		business.setBusDate(new Date(sj.getNetworkTime()));
		business.setBusTime(sj.getDaiXing(business.getBusKai(),business.getBusJie()));
		business.setBusXshen(a_ser.selectDeptStaff(staff_id).toString());
		b_dao.save(business);
	}
	public void shen(int id,int shen,String str) {
		KqBusiness pojo=b_dao.findById(id).get();
		if(shen==2) {
			pojo.setBusBou(str);
		}
		pojo.setBusSdate(new Date(sj.getNetworkTime()));
		pojo.setBusShen(shen);
		b_dao.save(pojo);
	}
	public Boolean selectbetween(int id,Date date) {
		if(map.selectbetween(id, date)>=1) {
			return true;
		}
		return false;
	}
}
